<template>
    <div style="margin-right: 30px;">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-col :span="12">
                <el-form-item label="用户名" prop="userName">
                    <el-input v-model="ruleForm.userName"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="密码" prop="password">
                    <el-input v-model="ruleForm.password"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="姓名" prop="fullName">
                    <el-input v-model="ruleForm.fullName"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="ruleForm.email"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24">
                <el-form-item label="头像">
                    <el-upload class="avatar-uploader" :action="uploadFileUrl" :show-file-list="false"
                        :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
            </el-col>
        </el-form>
    </div>
</template>

<script>
    import { Edit } from '@/api/admin';
    export default {
        name: 'RbacProjectUiAdminAdd',

        data() {
            return {
                ruleForm: {
                    userName: '',
                    password: '',
                    fullName: '',
                    email: '',
                    avatar: ''
                },
                rules: {
                    userName: [
                        { required: true, message: '请输入用户名', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ]
                },
                uploadFileUrl: `${process.env.VUE_APP_BASEURL}/UploadFile/Upload`,
                imageUrl: ''
            };
        },

        props: {
            rowData: {
                type: Object
            }
        },

        mounted() {
            Edit({ adminId: this.rowData.adminId }).then(m => {
                
            });
        },

        methods: {
            handleAvatarSuccess(res, file) {
                this.imageUrl = URL.createObjectURL(file.raw);
                this.ruleForm.avatar = res;
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg' || file.type === "image/png";
                const isLt2M = file.size / 1024 / 1024 < 20;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            SaveAdd() {
                this.$refs.ruleForm.validate((valid) => {
                    if (valid) {
                        var _this = this;
                        Add(this.ruleForm).then(m => {
                            this.$message({
                                message: '添加成功',
                                type: 'success',
                                onClose: function () {
                                    _this.$emit("Save");
                                }
                            });
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
        },
    };
</script>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 88px;
        height: 88px;
        line-height: 88px;
        text-align: center;
    }

    .avatar {
        width: 88px;
        height: 88px;
        display: block;
    }
</style>